<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="550px"
            :clickModalClose="true"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" label-width="84px" :rules="formRules" label-position="top">
                <el-form-item label="上级区域" prop="pid">
                    <el-tree-select
                        class="flex-1"
                        v-model="formData.pid"
                        :data="menuOptions"
                        clearable
                        node-key="id"
                        :props="{
                            label: 'name'
                        }"
                        :default-expand-all="true"
                        placeholder="请选择父级菜单"
                        check-strictly
                        :disabled="true"
                    />
                </el-form-item>
                <el-form-item label="地区名称" prop="name">
                    <el-input v-model="formData.name" placeholder="请输入地区名称" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import {  areaEdit, areaAdd, areaDetail } from '@/api/area'
import Popup from '@/components/popup/index.vue'
import feedback from '@/utils/feedback'
import type { PropType } from 'vue'
import {menuLists} from "@/api/perms/menu";
import {areaLevel} from "@/api/site";
import {arrayToTree, treeToArray} from "@/utils/util";
import {MenuEnum} from "@/enums/appEnums";
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑区域' : '新增区域'
})

const menuOptions = ref<any[]>([])

const getAreaLevel = async () => {
    const data: any = await areaLevel()
    const area: any = { id: 0, name: '顶级', children: [] }
    area.children = arrayToTree(treeToArray(data))
    menuOptions.value.push(area)
}

const formData = reactive({
    id: '',
    name: '',
    pid: 0,
    level: '',
})

const formRules = {
    name: [
        {
            required: true,
            message: '请输入地区名称',
            trigger: ['blur']
        }
    ],
    pid: [
        {
            required: true,
            message: '请输入上级地区id',
            trigger: ['blur']
        }
    ]
}

const handleSubmit = async () => {
    await formRef.value?.validate()
    const data: any = { ...formData }
    mode.value == 'edit' ? await areaEdit(data) : await areaAdd(data)
    popupRef.value?.close()
    feedback.msgSuccess('操作成功')
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = async (data: Record<string, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}

const getDetail = async (row: Record<string, any>) => {
    const data = await areaDetail({
        id: row
    })
    setFormData(data)
}

const handleClose = () => {
    emit('close')
}

getAreaLevel()

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
